package pl.plesken.swt;

import java.util.LinkedList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

import pl.plesken.languages.SetLanguage;
import pl.plesken.xml.GenresXML;

public class GenresTree extends Dialog {

	private Tree tree;
	protected Object result;
	protected Shell shell;
	LinkedList<String> row = new LinkedList<String>();
	StringBuffer line = new StringBuffer();


	public LinkedList<String> getRow() {
		return row;
	}

	public void setRow(LinkedList<String> row) {
		this.row = row;
	}

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public GenresTree(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public GenresTree(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		//ConfigXML xml = new ConfigXML();
		GenresXML xml = new GenresXML();
		
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new GridLayout());
		shell.setSize(479, 375);
		shell.setText(SetLanguage.getString("GenresTree.AppName")); //$NON-NLS-1$

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout());

		final Group group = new Group(composite, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		group.setLayout(gridLayout);

		tree = new Tree(group, SWT.BORDER);
		tree.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(final MouseEvent e) {
				edit();
			}
		});
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final TreeColumn genreTreeColumn = new TreeColumn(tree, SWT.NONE);
		genreTreeColumn.setWidth(200);

		final Group group_1 = new Group(group, SWT.NONE);
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		group_1.setLayout(new GridLayout());

		final Button addButton = new Button(group_1, SWT.NONE);
		addButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				addGenre();
			}
		});
		final GridData gd_addButton = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_addButton.widthHint = 60;
		addButton.setLayoutData(gd_addButton);
		addButton.setText(SetLanguage.getString("GenresTree.addGenre")); //$NON-NLS-1$

		final Button subGenreButton = new Button(group_1, SWT.NONE);
		subGenreButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				addSubGenre();
			}
		});
		subGenreButton.setToolTipText(SetLanguage.getString("GenresTree.subgenreInfo")); //$NON-NLS-1$
		final GridData gd_subGenreButton = new GridData(SWT.FILL, SWT.CENTER, true, false);
		subGenreButton.setLayoutData(gd_subGenreButton);
		subGenreButton.setText(SetLanguage.getString("GenresTree.addSubGenre")); //$NON-NLS-1$

		final Button delButton = new Button(group_1, SWT.NONE);
		delButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				delItem();
			}
		});
		final GridData gd_delButton = new GridData(SWT.FILL, SWT.CENTER, false, false);
		gd_delButton.widthHint = 60;
		delButton.setLayoutData(gd_delButton);
		delButton.setText(SetLanguage.getString("GenresTree.del")); //$NON-NLS-1$

		final Group group_2 = new Group(composite, SWT.NONE);
		group_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 2;
		group_2.setLayout(gridLayout_1);

		final Button setButton = new Button(group_2, SWT.NONE);
		setButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				setIt();
			}
		});
		final GridData gd_setButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_setButton.widthHint = 80;
		setButton.setLayoutData(gd_setButton);
		setButton.setText(SetLanguage.getString("GenresTree.set")); //$NON-NLS-1$

		final Button cancelButton = new Button(group_2, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.close();
			}
		});
		final GridData gd_cancelButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_cancelButton.widthHint = 80;
		cancelButton.setLayoutData(gd_cancelButton);
		cancelButton.setText(SetLanguage.getString("GenresTree.cancel")); //$NON-NLS-1$
		
		//xml.parseGenresXML();
		xml.getGenreValues();
			
		for (int i = 0; i < xml.getRow().size(); i++) {
			TreeItem treeItem = new TreeItem(tree, 0);
			String[] mainItem = xml.getRow().get(i).split(";"); //$NON-NLS-1$
			treeItem.setText(mainItem[0]);
			for (int s = 1; s < mainItem.length; s++) {
				TreeItem treeSub = new TreeItem(treeItem, 0);
				treeSub.setText(mainItem[s]);
			}
		}
		
	}
	
	private void addGenre() {
		GenreAdd add = new GenreAdd(shell);
		
		add.open();
		
		try {
			if (!add.getName().equals("")) {
				TreeItem treeItem = new TreeItem(tree, 0);
				treeItem.setText(add.getName());
			}
		} catch(IllegalArgumentException e) {
		} catch(NullPointerException e) {
		}
	}

	private void addSubGenre() {
		GenreAdd add = new GenreAdd(shell);
		
		add.open();
		
		try {
			if (!add.getName().equals("")) {
				TreeItem treeItem = new TreeItem(tree.getSelection()[0], 0);
				treeItem.setText(add.getName());
			}
		} catch(IllegalArgumentException e) {
		} catch(NullPointerException e) {
		}
	}

	private void delItem() {
		TreeItem[] item = tree.getSelection();

		item[0].dispose();
	}
	
	private void edit() {
		GenreEdit edit = new GenreEdit(shell);
		
		edit.setName(tree.getSelection()[0].getText().toString());
		
		edit.open();
		
		tree.getSelection()[0].setText(edit.getName());
	}
	
	private void setIt() {
		TreeItem[] root = tree.getItems();
		//ConfigXML xml = new ConfigXML();
		GenresXML xml = new GenresXML();
		
		for (int a = 0; a < root.length; a++) {
			line.setLength(0);
			line.append(tree.getItem(a).getText());
			TreeItem[] sub = tree.getItem(a).getItems();
			for (int i = 0; i < sub.length; i ++) {
				line.append(";" + tree.getItem(a).getItem(i).getText()); //$NON-NLS-1$
			}
			row.add(line.toString());
		}
		xml.setSave(row);
		//xml.saveGenresXML();
		xml.saveGenreValues();
		shell.close();
	}
}
